﻿using Common;
using Common.XmlHandle;
using Network.UpdateData;
using SFJDManagement.SocketHandle;
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace SFJDManagement
{
    /// <summary>
    /// ChoiceBrigade.xaml 的交互逻辑
    /// </summary>
    public partial class Win_Brigade : Window
    {
        //public static ChoiceBrigade instance;
        private ClassBrigade brigade;
        private Page_Register parent_Container;
        private List<ClassBrigade> brigadeList;

        public Win_Brigade(Page_Register win_Parent)
        {
            parent_Container = win_Parent;
            Win_Main2.instance.Visibility = Visibility.Hidden;
            Visibility = Visibility.Visible;
            InitializeComponent();
            //instance = this;
        }
        
        private void choiceBrigade_Loaded(object sender, RoutedEventArgs e)
        {
            brigadeList = Runtime.xmlBrigade.GetAll();
            dataGrid.ItemsSource = brigadeList;
        }

        private void dataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if(dataGrid.SelectedItem != null)
            {
                //ClassBrigade cBrigade = (ClassBrigade)dataGrid.CurrentCell.Item;
                brigade = (ClassBrigade)dataGrid.CurrentCell.Item; 
                textBox.Text = brigade.FullName;
            }
        }

        private void textBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            parent_Container.but_Brigade.Content = textBox.Text;
        }

        private void choiceBrigade_Closed(object sender, EventArgs e)
        {
            Close();
            Win_Main2.instance.Visibility = Visibility.Visible;
        }

        private void button_Click(object sender, RoutedEventArgs e)
        {
            parent_Container.SetBrigade(brigade);
            Close();
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            if (button1.Content.Equals("添加委托人"))
            {
                button.Visibility = Visibility.Hidden;
                //button.IsEnabled = false;
                button1.Content = "选择委托人";
                label.Visibility = Visibility.Hidden;
                textBox1.Visibility = Visibility.Hidden;
                button1.HorizontalAlignment = HorizontalAlignment.Center;
                tabControl.SelectedIndex = 1;
                textBox.Text = "请填写表格...";
            }
            else
            {
                button.Visibility = Visibility.Visible;
                //button.IsEnabled = true;
                button1.Content = "添加委托人";
                button1.HorizontalAlignment = HorizontalAlignment.Right;
                label.Visibility = Visibility.Visible;
                textBox1.Visibility = Visibility.Visible;
                tabControl.SelectedIndex = 0;
                textBox.Text = "请选择...";
            }
        }

        private void btAddBri_Click(object sender, RoutedEventArgs e)
        {
            if (btAddBri.Content.Equals("请稍等。。。"))
            {
                return;
            }
            if (!Regex.IsMatch(tbBriPostcode.Text, @"^\d{6}$"))
            {
                tbBriPostcode.Foreground = Brushes.Red;
                return;
            }
            if (!Regex.IsMatch(tbBriCode.Text, @"^[a-zA-Z]+$"))
            {
                tbBriCode.Foreground = Brushes.Red;
                return;
            }
            if (tbBriBrigade.Foreground == Brushes.Red | tbBriBrigade.Foreground == Brushes.Red |
                tbBriCity.Foreground == Brushes.Red | tbBriCode.Foreground == Brushes.Red |
                tbBriDistrict.Foreground == Brushes.Red | tbBriFullName.Foreground == Brushes.Red |
                tbBriPostcode.Foreground == Brushes.Red | tbBriProvince.Foreground == Brushes.Red)
            {
                return;
            }
            ClassBrigade newBrigade = new ClassBrigade();
            newBrigade.Guid = new Guid(tbBriGuid.Text);
            newBrigade.FullName = tbBriFullName.Text;
            newBrigade.Province = tbBriProvince.Text;
            newBrigade.City = tbBriCity.Text;
            newBrigade.District = tbBriDistrict.Text;
            newBrigade.Brigade = tbBriBrigade.Text;
            newBrigade.Code = tbBriCode.Text.ToUpper();
            newBrigade.Address = tbBriAddress.Text;
            newBrigade.Postcode = tbBriPostcode.Text;
            //new XmlBrigade(MainWindow.fileBrigade).AddBrigade(newBrigade);
            if(Runtime.xmlBrigade.IsDefindFullname(newBrigade.FullName))
            {
                tbBriFullName.Foreground = Brushes.Red;
                tbBriFullName.Text += " 这个委托人已经注册过";
                return;
            }
            if (Runtime.xmlBrigade.IsDefindCode(newBrigade.Code))
            {
                tbBriCode.Foreground = Brushes.Red;
                tbBriCode.Text += " 这个编码已使用";
                return;
            }
            XmlChinaList chinaList = new XmlChinaList(Runtime.programPath + "\\ChinaList.xml");
            if (!chinaList.IsThereProvince(newBrigade.Province))
            {
                tbBriProvince.Foreground = Brushes.Red;
                return;
            }
            if (!chinaList.IsThereCity(newBrigade.Province, newBrigade.City))
            {
                tbBriCity.Foreground = Brushes.Red;
                return;
            }
            if (!chinaList.IsThereDistrict(newBrigade.Province, newBrigade.City, newBrigade.District))
            {
                tbBriDistrict.Foreground = Brushes.Red;
                return;
            }
            Packet packet = new Packet(typeof(SC02RegisterBrigade).GetClient2ServerKey(), Runtime.config.UserGuid, newBrigade.Coding2String());
            CSendMsg.SendMsgNoWait(packet.ToBytes());
            //new CSendMsg().SendMsgWait("2030" + newBrigade.Coding2String());
            SwitchButton();
            new Thread(() =>
            {
                while(Runtime.xmlBrigade.GetBrigadeByGuid(newBrigade.Guid) == null)
                {
                    Thread.Sleep(200);
                }
                SwitchButton();
                brigadeList = Runtime.xmlBrigade.GetAll();
            }).Start();
        }

        internal void SwitchButton()
        {
            Dispatcher.Invoke(new Action(delegate
            {
                if(button.Visibility == Visibility.Hidden)
                {
                    button.Visibility = Visibility.Visible;
                    button1.Content = "添加委托人";
                    button1.HorizontalAlignment = HorizontalAlignment.Right;
                    label.Visibility = Visibility.Visible;
                    textBox1.Visibility = Visibility.Visible;
                    tabControl.SelectedIndex = 0;
                    textBox.Text = "请选择...";
                }
                else
                {
                    btAddBri.IsEnabled = false;
                    btAddBri.Content = "请稍等。。。";
                }
            }));
        }

        private void wrapPanel7_GotFocus(object sender, RoutedEventArgs e)
        {
            if(tbBriGuid.Text.Equals("自动生成"))
            {
                tbBriGuid.Text = Guid.NewGuid().ToString().ToUpper();
            }
        }

        private void tbBriProvince_GotFocus(object sender, RoutedEventArgs e)
        {
            tbBriProvince.Text = null;
            tbBriProvince.Foreground = Brushes.Black;
        }

        private void tbBriCity_GotFocus(object sender, RoutedEventArgs e)
        {
            tbBriCity.Text = null;
            tbBriCity.Foreground = Brushes.Black;
        }

        private void tbBriDistrict_GotFocus(object sender, RoutedEventArgs e)
        {
            tbBriDistrict.Text = null;
            tbBriDistrict.Foreground = Brushes.Black;
        }

        private void tbBriBrigade_GotFocus(object sender, RoutedEventArgs e)
        {
            tbBriBrigade.Text = null;
            tbBriBrigade.Foreground = Brushes.Black;
        }

        private void tbBriFullName_GotFocus(object sender, RoutedEventArgs e)
        {
            tbBriFullName.Text = null;
            tbBriFullName.Foreground = Brushes.Black;
        }

        private void tbBriFullName_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (!tbBriFullName.Text.Equals("示例：西安市公安局交警支队高新大队"))
            {
                textBox.Text = tbBriFullName.Text;
            }
        }

        private void tbBriCode_GotFocus(object sender, RoutedEventArgs e)
        {
            tbBriCode.Text = null;
            tbBriCode.Foreground = Brushes.Black;
        }

        private void tbBriAddress_GotFocus(object sender, RoutedEventArgs e)
        {
            tbBriAddress.Text = null;
            tbBriAddress.Foreground = Brushes.Black;
        }

        private void tbBriPostcode_GotFocus(object sender, RoutedEventArgs e)
        {
            tbBriPostcode.Text = null;
            tbBriPostcode.Foreground = Brushes.Black;
        }

        private void textBox1_TextChanged(object sender, TextChangedEventArgs e)
        {
            if(textBox1.Text.Length == 1 && (int)textBox1.Text.ToCharArray()[0] == 32)
            {
                textBox1.Text = null;
            }
            else if(!textBox1.Text.Equals("输入汉字或字母快速查找"))
            {
                List<ClassBrigade> brigadeList = Runtime.xmlBrigade.GetAll();
                dataGrid.ItemsSource = new SearchBrigade(brigadeList, textBox1.Text).getListBack();
            }
        }

        private void textBox1_GotFocus(object sender, RoutedEventArgs e)
        {
            //dataGrid.SelectedItem = null;
            textBox1.Text = null;
        }
    }
}
